<?php 
include_once("dataprovider.php");
class SanPham
{
	var $MaSanPham;
	var $TenSanPham;
	var $ThongTin;
	var $HinhSlide = '';
	var $DaXoa=0;
	
	
	public static function LayUrlHinhDaiDienChoSanPham($masp)
	{
		$sql = "
				SELECT *
				FROM hinhanh
				WHERE MaSanPham=".$masp."
		";
		$result = DataProvider::ExecuteQuery($sql);
		$row = mysql_fetch_array($result);
		if($row)
		{
			return $row["Url"];
		}
		return NULL;
	}
	
	public static function LayDanhSachSanPham_TatCa()
	{
		$sql = "
				SELECT *
				FROM sanpham
		";
		$result = DataProvider::ExecuteQuery($sql);
		$dsSanPham[] = new SanPham;
		$i = 0;
		while($row = mysql_fetch_array($result))
		{
			$sp = new SanPham;
			$sp->MaSanPham = $row["MaSanPham"];
			$sp->TenSanPham = $row["TenSanPham"];
			$sp->ThongTin = $row["ThongTin"];
			$sp->HinhSlide = $row["HinhSlide"];
			$sp->DaXoa = $row["DaXoa"];
			$dsSanPham[$i++]=$sp;
		}
		return $dsSanPham;
	}
	
	public static function LayDanhSachSanPham_ChuaXoa()
	{
		$sql = "
			SELECT *
			FROM sanpham
			WHERE DaXoa = 0
		";
		$result = DataProvider::ExecuteQuery($sql);
		$dsSanPham[] = new SanPham;
		if(!$result)
			return NULL;
		$i = 0;
		while($row = mysql_fetch_array($result))
		{
			$sp = new SanPham;
			$sp->MaSanPham = $row["MaSanPham"];
			$sp->TenSanPham = $row["TenSanPham"];
			$sp->ThongTin = $row["ThongTin"];
			$sp->HinhSlide = $row["HinhSlide"];
			$sp->DaXoa = $row["DaXoa"];
			$dsSanPham[$i++]=$sp;
		}
		return $dsSanPham;
	}
	
	public static function LayDanhSachSanPham_DaXoa()
	{
		$sql = "
			SELECT *
			FROM sanpham
			WHERE DaXoa = 1
		";
		$result = DataProvider::ExecuteQuery($sql);
		$dsSanPham[] = new SanPham;
		if(!$result)
			return NULL;
		$i = 0;
		while($row = mysql_fetch_array($result))
		{
			$sp = new SanPham;
			$sp->MaSanPham = $row["MaSanPham"];
			$sp->TenSanPham = $row["TenSanPham"];
			$sp->ThongTin = $row["ThongTin"];
			$sp->HinhSlide = $row["HinhSlide"];
			$sp->DaXoa = $row["DaXoa"];
			$dsSanPham[$i++]=$sp;
		}
		return $dsSanPham;
	}
	
	
	
	public static function LaySanPham_ChuaXoa_TheoMa($masp)
	{
		$sql = "
			SELECT *
			FROM sanpham
			WHERE MaSanPham = ".$masp." AND DaXoa=0
		";
		$result = DataProvider::ExecuteQuery($sql);
		$row = mysql_fetch_array($result);
		if($row)
		{
			$sp = new SanPham;
			$sp->MaSanPham = $row["MaSanPham"];
			$sp->TenSanPham = $row["TenSanPham"];
			$sp->ThongTin = $row["ThongTin"];
			$sp->HinhSlide = $row["HinhSlide"];
			$sp->DaXoa = $row["DaXoa"];
			return $sp;
		}
		return NULL;
	}
	
	public static function LaySanPham_DaXoa_TheoMa($masp)
	{
		$sql = "
			SELECT *
			FROM sanpham
			WHERE MaSanPham = ".$masp." AND DaXoa=1
		";
		$result = DataProvider::ExecuteQuery($sql);
		$row = mysql_fetch_array($result);
		if($row)
		{
			$sp = new SanPham;
			$sp->MaSanPham = $row["MaSanPham"];
			$sp->TenSanPham = $row["TenSanPham"];
			$sp->ThongTin = $row["ThongTin"];
			$sp->HinhSlide = $row["HinhSlide"];
			$sp->DaXoa = $row["DaXoa"];
			return $sp;
		}
		return NULL;
	}
	
	
	
	public static function ThemVaLayMaSanPhamLonNhat()
	{
		$sql = "
			INSERT INTO sanpham(MaSanPham) VALUE('')
		";
		DataProvider::ExecuteQuery($sql);
		$sql = "
			SELECT max(MaSanPham)
			FROM sanpham
		";
		$result = DataProvider::ExecuteQuery($sql);
		$MaxID = mysql_fetch_array($result);
		return $MaxID[0];
	}
	
	public static function ThemSanPham($sp)
	{
		$sql = "
			INSERT INTO sanpham(TenSanPham,ThongTin,HinhSlide,DaXoa)
			VALUE(	'".$sp->TenSanPham."',
					'".$sp->ThongTin."',
					'".$sp->HinhSlide."',
					0	)
		";
		$result = DataProvider::ExecuteQuery($sql);
		return $result;
	}
	
	public static function XoaSanPhamTheoMa($masp)
	{
		$sql = "
			UPDATE sanpham
			SET DaXoa = 1
			WHERE MaSanPham = ".$masp."
		";
		$result = DataProvider::ExecuteQuery($sql);
		return $result;
	}
	
	public static function CapNhatSanPham(SanPham $sp)
	{
		$sql = "
			UPDATE sanpham
			SET TenSanPham='".$sp->TenSanPham."',
				ThongTin='".$sp->ThongTin."',
				HinhSlide='".$sp->HinhSlide."',
				DaXoa=".$sp->DaXoa."
			WHERE MaSanPham=".$sp->MaSanPham."
		";
		$result = DataProvider::ExecuteQuery($sql);
		return $result;
	}
}
?>